import requests
import logging
import pytest
import json
def readjson():
    with open("dlsjson.json","r",encoding="utf-8") as f:
        data=json.load(f)
        print(data)
        return data
class TestAddDaiLiShang():
    token=""
    def setup_method(self):
        print("初始化环境")
        print("用例开始执行")
    def teardown_method(self):
        print("清理环境")
        print("用例结束执行")
    #登录接口，提取token
    def test_login(self):
        try:
            url="http://10.59.9.21/api/user/login"
            data={"username":"admin","password":"8i4De25KlVQVwqTDgTNGqw=="}
            r = requests.post(url=url,json=data)
            print("登录接口：",r.json())
            TestAddDaiLiShang.token=r.json()['token']
            logging.info(r.json())
        except Exception as e:
            logging.info("出错了：",e)

    # 测试点001: 接口参数填写正确，成功新增代理商。
    @pytest.mark.parametrize("data1",readjson())
    def test_case001_addDLS(self,data1):
        try:
            url="http://10.59.9.21/api/user/register"
            data={"username":data1.get('username'),"password":"+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
                  "checkPwd":"+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
                  "a_id":data1.get('a_id'),"dtype":2,"phone":"13244445555","deadline":"2025-12-30"}
            headers={'token':TestAddDaiLiShang.token,"uid":"1"}
            r=requests.post(url=url,json=data,headers=headers)
            print(r.json())
            logging.info(r.json())

        except Exception as e:
            logging.info(e)
        # print(data1.get('duanyan'))
        assert data1.get('duanyan') in r.text


    # #   测试点002: 用户名已存在，注册失败
    # def test_case002_addDLS(self):
    #     try:
    #         url = "http://10.59.9.21/api/user/register"
    #         data = {"username": 'zhangniuniu008', "password": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
    #                 "checkPwd": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=", "a_id": "zhangniuniu0080", "dtype": 2,
    #                 "phone": "13244445555", "deadline": "2025-12-30"}
    #         headers = {'token': TestAddDaiLiShang.token, "uid": "1"}
    #         r = requests.post(url=url, json=data, headers=headers)
    #         print(r.json())
    #         logging.info(r.json())
    #     except Exception as e:
    #         print(e)
    #     assert r.json()['code'] == 1062
    #     assert r.status_code == 200
    #     assert '用户名已存在' in r.text
    # #   测试点003: 用户名为中文，注册失败
    # def test_case003_addDLS(self):
    #     try:
    #         url = "http://10.59.9.21/api/user/register"
    #         data = {"username": 'zhang牛牛', "password": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
    #                 "checkPwd": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=", "a_id": "zhangniuniu0081", "dtype": 2,
    #                 "phone": "13244445555", "deadline": "2025-12-30"}
    #         headers = {'token': TestAddDaiLiShang.token, "uid": "1"}
    #         r = requests.post(url=url, json=data, headers=headers)
    #         print(r.json())
    #         logging.info(r.json())
    #     except Exception as e:
    #         print(e)
    #     assert r.json()['codeMsg'] == 'INCORRECT FORMAT'
    #     assert r.status_code == 200
    #     assert '只能输入字母、数字' in r.text
    # # 测试点004: 用户名为空，注册失败
    # def test_case004_addDLS(self):
    #     try:
    #         url = "http://10.59.9.21/api/user/register"
    #         data = {"username": '', "password": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
    #                 "checkPwd": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=", "a_id": "zhangniuniu0081", "dtype": 2,
    #                 "phone": "13244445555", "deadline": "2025-12-30"}
    #         headers = {'token': TestAddDaiLiShang.token, "uid": "1"}
    #         r = requests.post(url=url, json=data, headers=headers)
    #         print(r.json())
    #         logging.info(r.json())
    #     except Exception as e:
    #         print(e)
    #     assert r.json()['codeMsg'] == 'NOT NULL VALUE'
    #     assert r.status_code == 200
    #     assert '用户名不能为空' in r.text
    # # 测试点005: 用户名含有特殊符号，注册失败
    # def test_case004_addDLS(self):
    #     try:
    #         url = "http://10.59.9.21/api/user/register"
    #         data = {"username": '#%￥#￥%#', "password": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=",
    #                 "checkPwd": "+NParMl8hnIwJtGfrwAEaQu+UgLUZ28IJggmPy887Ys=", "a_id": "zhangniuniu0081", "dtype": 2,
    #                 "phone": "13244445555", "deadline": "2025-12-30"}
    #         headers = {'token': TestAddDaiLiShang.token, "uid": "1"}
    #         r = requests.post(url=url, json=data, headers=headers)
    #         print(r.json())
    #         logging.info(r.json())
    #     except Exception as e:
    #         print(e)
    #     assert r.json()['codeMsg'] == 'INCORRECT FORMAT'
    #     assert r.status_code == 200
    #     assert '请输入4到16位' in r.text